package controllers

import (
	"lottery/comm"
	"lottery/models"
)

// 将获得大奖的 ip 加入黑名单
func (idx *IndexController) prizeLarge(ip string, loginuser *models.ObjLoginuser,
	userinfo *models.LtUser, blackipInfo *models.LtBlackIp) {
	// 时间范围
	nowTime := comm.NowUnix()
	blackTime := 30 * 86400 // 设置一个月黑名单
	// 1. 更新用户的黑名单信息
	// 如果数据不存在
	if userinfo == nil || userinfo.Id <= 0 {
		userinfo = &models.LtUser{
			Id:         loginuser.Uid,
			Username:   loginuser.Username,
			BlackTime:  nowTime + blackTime,
			SysUpdated: 0,
		}
		// 创建用户
		idx.ServiceUser.Create(userinfo)
	} else {
		userinfo = &models.LtUser{
			Id:         loginuser.Uid,
			BlackTime:  nowTime + blackTime,
			SysUpdated: nowTime,
		}
		// 执行更新
		idx.ServiceUser.Update(userinfo, nil)
	}

	// 2. 更新ip黑名单信息
	if blackipInfo == nil || blackipInfo.Id <= 0 {
		blackipInfo = &models.LtBlackIp{
			Ip:         ip,
			BlackTime:  nowTime + blackTime,
			SysCreated: nowTime,
		}
		idx.ServiceBlackip.Create(blackipInfo)
	} else {
		blackipInfo.BlackTime = nowTime + blackTime
		blackipInfo.SysUpdated = nowTime
		idx.ServiceBlackip.Update(blackipInfo, nil)
	}
}
